
import {
  SET_HEAD_TITLE,
  SET_USER,
  RESET_USER
} from './actions-types'

import { login } from '../api/login'
import { message } from 'antd'
import storageUtils from '../utils/storageUtils'

export const setHeadTitle = (headTtile) => ({ type: SET_HEAD_TITLE, data: headTtile })

export const receiveUser = (user) => ({ type: SET_USER, data: user })

export const asynclogin = (username, password) => {
  return async dispatch => {
    const result = await login(username, password)
    if (result.status === 0) {
      const user = result.data
      storageUtils.saveUser(user)
      dispatch(receiveUser(user))
    } else {
      const errorMsg = result.msg
      message.error(errorMsg)
    }
  }
}

export const logout = () => {
  storageUtils.removeUser()
  return { type: RESET_USER }
}